package main

import (
	"TestGo/File/copy"
	"fmt"
)

func main() {
	//第一种
	//file, err := os.Open("D:/GoCode/test.txt")
	//if err != nil {
	//	fmt.Println("open file err=", err)
	//}
	//defer file.Close()
	//
	//reader := bufio.NewReader(file)
	//for {
	//	str, err := reader.ReadString('\n')
	//	if err == io.EOF {
	//		break
	//	}
	//	fmt.Print(str)
	//}
	////fmt.Printf("file=%v", file)
	//err = file.Close()
	//if err != nil {
	//	fmt.Println("file close err=", err)
	//}

	//第二种一次性读取（不大的文件）
	//file := "D:/GoCode/test.txt"
	//content, err := ioutil.ReadFile(file)
	//if err != nil {
	//	fmt.Printf("read file err=%v", err)
	//}
	//fmt.Printf("%v", string(content))
	//

	//写文件（1）
	//filePath := "D:/GoCode/test.txt"
	//file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
	//if err != nil {
	//	fmt.Printf("open file err=%v\n", err)
	//	return
	//}
	//defer file.Close()
	//str := "abcderg\n"
	//writer := bufio.NewWriter(file)
	//for i := 0; i < 5; i++ {
	//	writer.WriteString(str)
	//}
	//writer.Flush()

	//追加
	//filePath := "D:/GoCode/test.txt"
	//file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666)
	//if err != nil {
	//	fmt.Printf("open file err=%v\n", err)
	//	return
	//}
	//defer file.Close()
	//str := "summer is handsome\n"
	//writer := bufio.NewWriter(file)
	//for i := 0; i < 5; i++ {
	//	writer.WriteString(str)
	//}
	//writer.Flush()

	//读与写
	//filePath := "D:/GoCode/test.txt"
	//file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND, 0666)
	//if err != nil {
	//	fmt.Printf("open file err=%v\n", err)
	//	return
	//}
	//defer file.Close()

	//读取
	//reader := bufio.NewReader(file)
	//for {
	//	str, err := reader.ReadString('\n')
	//	if err == io.EOF {
	//		break
	//	}
	//	fmt.Print(str)
	//}
	////写
	//str := "summer is studying\n"
	//writer := bufio.NewWriter(file)
	//for i := 0; i < 5; i++ {
	//	writer.WriteString(str)
	//}
	//writer.Flush()

	//将一个文件内容写到另一个文件中
	//filePath := "D:/GoCode/test.txt"
	//NFilePath := "D:/GoCode/test1.txt"
	//content, err := ioutil.ReadFile(filePath)
	//if err != nil {
	//	fmt.Println("read file err=", err)
	//	return
	//}
	//err = ioutil.WriteFile(NFilePath, content, 0666)
	//if err != nil {
	//	fmt.Println("write file err=", err)
	//}

	//拷贝文件
	srcFile := "D:/GoCode/a.jpg"
	dstFile := "D:/GoCode/b.jpg"
	_, err := copy.CopyFile(dstFile, srcFile)
	if err == nil {
		fmt.Println("拷贝完成")
	} else {
		fmt.Println("拷贝失败")
	}
}
